#!/usr/bin/env python
#-*-coding:utf-8-*-

# http://www.cngold.com.cn/q/syjgzs.html
# http://www.cngold.com.cn/q/Handlers/Market/GetHqData.ashx
# groupcode:NYJGZS | pageSize:60 | pageIndex:1

from urllib import request
import urllib
# import http.cookiejar
import json

def main():
	# cj = http.cookiejar.LWPCookieJar()
	# handler = urllib.request.HTTPCookieProcessor(cj)  
	# opener = urllib.request.build_opener(handler, urllib.request.HTTPHandler)  
	# urllib.request.install_opener(opener) 
	# url = "http://www.cngold.com.cn/q/syjgzs.html"
	# # url = "http://python.jobbole.com/81344/"
	# req = request.Request(url)
	# req = request.Request("http://www.douban.com")
	# req.add_header("Accept", 
	# 	"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	# req.add_header("User-Agent", 
	# 	"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36")

	# req.add_header("Accept-Encoding","gzip, deflate, sdch, br")
	# with request.urlopen(req) as f:
	# 	print("status >>> ", f.status, f.reason, flush = True)
	# 	# print("*** headers *****")
	# 	# for k,v in f.getheaders():
	# 	# 	print("%s: %s" % (k,v))
	# 	# print("*****************")

	# 	# print("data: ", f.read())

	# for c in cj:
	# 	print(c)

	postdata = {"groupcode": "NYJGZS", "pageSize": 60, "pageIndex": 1}
	headers = {
		"Accept": "*/*", 
		"Accept-Encoding": "gzip, deflate",
		"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
		"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36",
		"X-Requested-With": "XMLHttpRequest"
	}

	postdata_bytes = urllib.parse.urlencode(postdata).encode('utf-8') 
	posturl = "http://www.cngold.com.cn/q/Handlers/Market/GetHqData.ashx"
	req2 = urllib.request.Request(posturl, postdata_bytes, headers)  
	print(req2)   
	try:
		rsp = urllib.request.urlopen(req2)  
		text = rsp.read().decode("utf-8")
		obj = json.loads(text)
		print(obj)

	except Exception as e:
		print("http error:", e)



if __name__ == '__main__':
	main()